"""
#!/usr/bin/python3
# coding:utf-8            
#
# Copyright (C) 2024 - 2024 Jasonakeke， Inc. All Rights Reserved 
# @Desc	   ：
# @Time    : 2024/8/14 22:12
# @Author  : Code_By_Jasonakeke
# @Email   : 2284037977@qq.com
# @File    : 01-创建ORM映射.py
# @IDE     : PyCharm
"""
from sqlalchemy import create_engine, text, Column, Integer, String
from sqlalchemy.orm import declarative_base

# 数据库的配置变量
HOSTNAME = '127.0.0.1'
PORT = '3306'
DATABASE = 'test'
USERNAME = 'root'
PASSWORD = '123456'
DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)


# 创建数据库引擎
engine = create_engine(DB_URI)

# 测试数据库连接
# with engine.connect() as conn:
# 	result = conn.execute(text('select * from t_pay'))
# 	print(result.fetchone())

# 第一步：创建一个基类（所有 ORM 中的 O，对象模型的超级父类）
Base = declarative_base()

# 第二步：定义 Python 类和表的映射
class Person(Base):
	__tablename__ = 't_person'
	id = Column(name = 'id', type_ = Integer, primary_key = True, autoincrement = True)
	name = Column(name = 'name', type_ = String(255))
	age = Column(name = 'age', type_ = Integer)
	address = Column(type_ = String(255))
	# 创建表之后新加的
	country = Column(type_ = String(50))
	city = Column(type_ = String(50))


# 第三步：创建表
Base.metadata.drop_all(bind = engine)
Base.metadata.create_all(bind = engine)
